package db_meta

import (
	"engine/db"
	"servers/models/define"
	"servers/models/protos/google/pb/rpc_protos"

	"gitee.com/ameise84/e3utils/str_conv"
	jsoniter "github.com/json-iterator/go"
)

func init() {
	db.RegisterTableMetaFactory(TableChairInfoFactory)
}

func TableChairInfoFactory() db.TableMeta {
	return &StatTableChairInfo{}
}

type StatTableChairInfo struct {
	UserID       define.UserIDType  `json:"id" form:"id" gorm:"primarykey;column:id"`
	IsSitDown    bool               `json:"is_sit_down" form:"is_sit_down" gorm:"column:is_sit_down"`
	GameKindID   define.GameKind    `json:"game_kind" form:"game_kind" gorm:"column:game_kind"`
	GameRoomName string             `json:"room_name" form:"room_name" gorm:"column:room_name"`
	GameTableID  define.TableIDType `json:"table_id" form:"table_id" gorm:"column:table_id"`
	GameChairID  define.ChairIDType `json:"chair_id" form:"chair_id" gorm:"column:chair_id"`
	LoginTime    int64              `json:"login_time" form:"login_time" gorm:"column:login_time"`
	LoginScore   define.GameScore   `json:"login_score" form:"login_score" gorm:"column:login_score"`
	LoginIP      string             `json:"login_ip" form:"login_ip" gorm:"column:login_ip"`
}

func (v *StatTableChairInfo) Init() {
	v.UserID = 0
	v.IsSitDown = false
	v.GameKindID = 0
	v.GameRoomName = ""
	v.GameTableID = 0
	v.GameChairID = 0
	v.LoginTime = 0
	v.LoginScore = 0
	v.LoginIP = ""
}

func (v *StatTableChairInfo) Reset(info *AcPlayer) {
	v.Init()
	v.UserID = info.UserID
}

func (v *StatTableChairInfo) BaseTableName() string {
	return v.TableName()
}

func (v *StatTableChairInfo) TableName() string {
	return "stat_player_table_info"
}

func (v *StatTableChairInfo) ID() string {
	return str_conv.ToString(v.UserID)
}

func (v *StatTableChairInfo) MarshalBinary() (data []byte, err error) {
	return jsoniter.Marshal(v)
}

func (v *StatTableChairInfo) UnmarshalBinary(data []byte) error {
	return jsoniter.Unmarshal(data, v)
}

func (v *StatTableChairInfo) ToPBMeta(meta *rpc_protos.TableChairInfoMeta) {
	meta.UserID = v.UserID
	meta.IsSitDown = v.IsSitDown
	meta.GameKindID = v.GameKindID
	meta.GameRoomName = v.GameRoomName
	meta.GameTableID = v.GameTableID
	meta.GameChairID = v.GameChairID
	meta.LoginTime = v.LoginTime
	meta.LoginScore = v.LoginScore
	meta.LoginIP = v.LoginIP
}

func (v *StatTableChairInfo) FromPBMeta(meta *rpc_protos.TableChairInfoMeta) {
	v.UserID = meta.UserID
	v.IsSitDown = meta.IsSitDown
	v.GameKindID = meta.GameKindID
	v.GameRoomName = meta.GameRoomName
	v.GameTableID = meta.GameTableID
	v.GameChairID = meta.GameChairID
	v.LoginTime = meta.LoginTime
	v.LoginScore = meta.LoginScore
	v.LoginIP = meta.LoginIP
}

func (v *StatTableChairInfo) EqualGame(meta *rpc_protos.TableChairInfoMeta) bool {
	return v.GameKindID == meta.GameKindID
}

func (v *StatTableChairInfo) EqualTable(meta *rpc_protos.TableChairInfoMeta) bool {
	return v.GameTableID == meta.GameTableID && v.GameChairID == meta.GameChairID && v.GameRoomName == meta.GameRoomName
}

func (v *StatTableChairInfo) Equal(meta *rpc_protos.TableChairInfoMeta) bool {
	return v.EqualGame(meta) && v.EqualTable(meta)
}
